import 'dart:io';

import 'package:ffmpeg_plugin/ffmpeg_plugin.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:path_provider/path_provider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  VideoUtil util = VideoUtil();
  List<FileSystemEntity> picList;
  List<File> picFiles = [];

  @override
  void initState() {
    super.initState();
    initPlatformState();
    util.initListener(success: () {
      print("success");
      printFileName();
    }, cancel: () {
      print("cancel");
    }, error: (message) {
//      Directory tempDir = await ;
//      printFileName();

      print("error:$message");
    }, progressCallBack: (ProgressData data) {
      print("progressCallBack");
      print(data);
    });
  }

  void printFileName() {
    getTemporaryDirectory().then((value) {
      String tempPath = value.path;
      var file = Directory("$tempPath/pics/");
      List<FileSystemEntity> files = file.listSync();
      picFiles.clear();
      files.forEach((element) {
        picFiles.add(element);
//        print(element.path);
      });
      setState(() {
        picFiles.sort((a, b) => a.path.compareTo(b.path));
      });
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            FlatButton(
                onPressed: () async {
                  Directory tempDir = await getTemporaryDirectory();
                  String tempPath = tempDir.path;
                  FilePickerResult result =
                      await FilePicker.platform.pickFiles(type: FileType.video);

                  VideoInfoData data =
                      await util.getInfo(result.files.first.path);
                  print(data);
                  util.exCommend(
                      " -i ${result.files.first.path} -c:v libx264 -preset superfast -x264opts keyint=25 -acodec copy -f mp4 $tempPath/test.mp4");
                  util.compressVideoAuto(
                      result.files.first.path, "$tempPath/test.mp4");
                },
                child: Text("选择")),
            FlatButton(
                onPressed: () async {
                  util.cancel();
                },
                child: Text("取消")),
            FlatButton(
                onPressed: () async {
                  Directory tempDir = await getTemporaryDirectory();
                  String tempPath = tempDir.path;
                  FilePickerResult result =
                      await FilePicker.platform.pickFiles(type: FileType.video);
                  VideoInfoData data =
                      await util.getInfo(result.files.first.path);
                  print(data);
                  picList = await util.cutVideoPreFrame(
                      result.files.first.path, "$tempPath/pics/",
                      purSecond: 5);
                },
                child: Text("视频帧")),
            FlatButton(
                onPressed: () async {
                  Directory tempDir = await getTemporaryDirectory();
                  String tempPath = tempDir.path;
                  FilePickerResult result =
                      await FilePicker.platform.pickFiles(type: FileType.video);
//                  VideoInfoData data =
//                  await util.getInfo(result.files.first.path);
                  //  print(data);
                  picList = await util.cutVideoFromTimeAdd(
                    result.files.first.path,
                    "$tempPath/test.mp4",
                    startTime: "00:00:02.000",
                    timeLong: "00:00:10.000",
                  );
                },
                child: Text("裁剪")),
            Container(
              height: 100,
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemBuilder: (ctx, index) => Stack(
                  children: [
                    Image.file(picFiles[index]),
                    Container(
                      child: Center(
                          child: Text(index.toString(),
                              style: TextStyle(color: Colors.white))),
                      color: Colors.black12,
                    )
                  ],
                ),
                itemCount: picFiles.length,
              ),
              color: Colors.red,
            )
          ],
        ),
      ),
    );
  }
}
